// ================================================================================
// 		File: PatyEduTestRecord.cs
// 		Desc: 
//  
// 		Called by:   
//               
// 		Auth: chenwenzhi（--）
// 		Date: 2015-12-27 22:18
// ================================================================================
// 		Change History
// ================================================================================
// 		Date:		Author:				Description:
// 		--------	--------			-------------------
//    
// ================================================================================
// 
// ================================================================================
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using MySql.Data.MySqlClient;
using ZHDJ.Data.Models.Box;
using ZHDJ.Data.Models.Org;

namespace ZHDJ.Data.Models.Edu
{
    /// <summary>
    /// 学习测试记录
    /// </summary>   
    public class PartyEduTestRecord :EntityBaseOperation
    {
        #region 属性
        
        /// <summary>
        /// 专题ID
        /// </summary>
        public string SpecialId { get; set; }

        /// <summary>
        /// 提交人
        /// </summary>
        public string SubimtAuthor { get; set; }
		
        /// <summary>
        /// 提交时间
        /// </summary>
        public DateTime? SubmitTime { get; set; }
		
        /// <summary>
        /// 是否答对所有
        /// </summary>
        public int IsAnswerAll { get; set; }

        public virtual ICollection<PartyEduTestRecordDetail> PatyEduTestRecordDetails { get; set; }
        public virtual PartyEduSpecial PatyEduSpecial { get; set; }

        #endregion

        #region 数据操作
        /// <summary>
        /// 在一分钟内是否提交用同样题目
        /// </summary>
        /// <param name="loginId"></param>
        /// <param name="subjectIds"></param>
        /// <returns></returns>
        public static bool IsTested(string loginId, List<string> subjectIds)
        {
            var para = new List<MySqlParameter>
            {
                new MySqlParameter()
                {
                    DbType = DbType.String,
                    ParameterName = "LoginID",
                    Value = loginId
                }
            };
            var sql = new StringBuilder(@"
              SELECT SubjectID from tblpartyedutestrecorddetail WHERE recordID=( select ID from tblpartyedutestrecord 
                where SubimtAuthor=?LoginID AND (UNIX_TIMESTAMP(NOW())-UNIX_TIMESTAMP(SubmitTime))/60<1 
                ORDER BY SubmitTime DESC LIMIT 1) GROUP BY SubjectID;  ");
            var dt= MySqlHelper.ExecuteDataTable(sql.ToString(), para.ToArray());
            var tested = (from DataRow dr in dt.Rows select Convert.ToString(dr["SubjectID"])).ToList();
            if (subjectIds.Count != tested.Count)
            {
                return false;
            }
            //差集
            var rs = tested.Except(subjectIds);
            return rs.Count()==0;
        }
        #endregion

    }
}
